package com.school.campus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.school.common.annotation.Log;
import com.school.common.core.controller.BaseController;
import com.school.common.core.domain.AjaxResult;
import com.school.common.enums.BusinessType;
import com.school.campus.domain.Repair;
import com.school.campus.service.IRepairService;
import com.school.common.utils.poi.ExcelUtil;
import com.school.common.core.page.TableDataInfo;

/**
 * 在线报修Controller
 *
 * @author wwj
 * @date 2025-11-03
 */
@RestController
@RequestMapping("/campus/repair")
public class RepairController extends BaseController {
    @Autowired
    private IRepairService repairService;

    /**
     * 查询在线报修列表
     */
    @PreAuthorize("@ss.hasPermi('campus:repair:list')")
    @GetMapping("/list")
    public TableDataInfo list(Repair repair) {
        startPage();
        List<Repair> list = repairService.selectRepairList(repair);
        return getDataTable(list);
    }

    /**
     * 导出在线报修列表
     */
    @PreAuthorize("@ss.hasPermi('campus:repair:export')")
    @Log(title = "在线报修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Repair repair) {
        List<Repair> list = repairService.selectRepairList(repair);
        ExcelUtil<Repair> util = new ExcelUtil<Repair>(Repair.class);
        util.exportExcel(response, list, "在线报修数据");
    }

    /**
     * 获取在线报修详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:repair:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(repairService.selectRepairById(id));
    }

    /**
     * 新增在线报修
     */
    @PreAuthorize("@ss.hasPermi('campus:repair:add')")
    @Log(title = "在线报修", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Repair repair) {
        return toAjax(repairService.insertRepair(repair));
    }

    /**
     * 修改在线报修
     */
    @PreAuthorize("@ss.hasPermi('campus:repair:edit')")
    @Log(title = "在线报修", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Repair repair) {
        return toAjax(repairService.updateRepair(repair,0L));
    }

    /**
     * 删除在线报修
     */
    @PreAuthorize("@ss.hasPermi('campus:repair:remove')")
    @Log(title = "在线报修", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(repairService.deleteRepairByIds(ids));
    }

    /**
     *  接单，处理报修
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('campus:repair:takeOrder')")
    @Log(title = "在线报修", businessType = BusinessType.UPDATE)
    @PutMapping("/takeOrder/{id}")
    public AjaxResult takeOrder(@PathVariable Long id) {
        Repair repair = repairService.selectRepairById(id);
        return toAjax(repairService.updateRepair(repair,1L));
    }


    /**
     * 完成报修修复
     */
    @PreAuthorize("@ss.hasPermi('campus:repair:complete')")
    @Log(title = "在线报修", businessType = BusinessType.UPDATE)
    @PutMapping("/complete/{id}")
    public AjaxResult complete(@PathVariable Long id){
        Repair repair = repairService.selectRepairById(id);
        return toAjax(repairService.updateRepair(repair,2L));
    }

}
